package com.social.itba.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.google.gson.Gson;
import com.social.itba.model.Movie;
import com.social.itba.service.MovieService;
import com.thoughtworks.xstream.XStream;

@Path("/movie")
public class MovieResource {

	@GET
	@Produces("application/xml")
	public String getAllMoviesXML(){
		XStream xstream= new XStream();
		xstream.alias("movie", Movie.class);
		
	    return xstream.toXML(MovieService.getAll());
	}
	
	@GET
	@Produces("application/json")
	public String getAllMoviesJSON(){
		Gson gson = new Gson();
	    String json = gson.toJson(MovieService.getAll());
	   
	    return json;
	}
	
	@GET
	@Path("{1}")
	@Produces("application/xml")
	public String getMovieXML(@PathParam("1") Integer id){
		XStream xstream= new XStream();
		xstream.alias("movie", Movie.class);
				
		return xstream.toXML(MovieService.get(id));
	}
	
	@GET
	@Path("{1}")
	@Produces("application/json")
	public String getMovieJSON(@PathParam("1") Integer id){
		Gson gson = new Gson();
	    String json = gson.toJson(MovieService.get(id));
				
		return json;
	}
	
	
	@PUT
    @Consumes("application/xml")
	public void putMovieXML(String content){
		XStream xstream= new XStream();
		xstream.alias("movie", Movie.class);
		
		Movie movie= (Movie)xstream.fromXML(content);		
	    MovieService.add(movie);
	}
	
	@POST
    @Consumes("application/xml")
	public void postMovieXML(String content){
		XStream xstream= new XStream();
		xstream.alias("movie", Movie.class);
		
		Movie movie= (Movie)xstream.fromXML(content);		
	    MovieService.add(movie);
	}
	
	@POST
    @Consumes("application/json")
	public void postMovieJSON(String content){
		Gson gson= new Gson();
		
		Movie movie= (Movie)gson.fromJson(content, Movie.class);		
	    MovieService.add(movie);
	}
}
